// pages/category/index.js
import {request} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMenuList:[],
    rightContent:[],
    currentIndex:0,
    Cates:[],
    scrollTop:0
  },
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const Cates = wx.getStorageSync("cates");
    if(!Cates){
       this.getCates()
    }else{
      if(Date.now()-Cates.time>1000*10){
        this.getCates()

      }else{
        this.Cates = Cates.data;
        let leftMenuList = this.Cates.map(v=>v.cat_name);
         let rightContent = this.Cates[0].children;
         this.setData({
          leftMenuList,
          rightContent
        })
      }
    }
   

  },

  // 商品分类数据
  async getCates(){
    // request({ url: "/categories" })
    // .then(result =>{

    //      this.Cates=result.data.message;

    //      wx.setStorageSync("cates", { time: Date.now(), data: this.Cates });
    //      let leftMenuList = this.Cates.map(v=>v.cat_name);
    //      let rightContent = this.Cates[0].children;
    //      this.setData({
    //       leftMenuList,
    //       rightContent
    //     })
    //      })

    const res = await request({url:"/categories"})
         this.Cates=res;

         wx.setStorageSync("cates", { time: Date.now(), data: this.Cates });
         let leftMenuList = this.Cates.map(v=>v.cat_name);
         let rightContent = this.Cates[0].children;
         this.setData({
          leftMenuList,
          rightContent
        })

  },

  //点击切换
  handleItemTap(e){
    //console.log(e)
    const {index} = e.currentTarget.dataset

    let rightContent = this.Cates[index].children;
    this.setData({
     currentIndex:index,
     rightContent,
     scrollTop:0
   })


  }

  
})